<?php

namespace App\Service\Activity;

use App\Helpers\AliyunUtil;
use App\Model\GiftLog;
use App\Service\BaseService;
use App\Model\UserProfile;

class DragonService extends BaseService
{
    /**
     * 端午节
     * @return array
     */
    public function handle()
    {
        $profileId   = $this->request->input('profile_id', '');
        $act         = $this->request->input('act', '');

        $selfProfile = !$profileId ? null : UserProfile::getInfoByProfileId($profileId);
        $userId      = $selfProfile ? $selfProfile->user_id : 0;

        $giftId       = 192;
        $time         = $act == '1' ? ['2023-06-19', '2023-06-26'] : ['2023-06-26', '2023-07-03'];
        $rankInfo     = GiftLog::giftRankList($giftId, 30, $userId, $time[0], $time[1]);
        $userIds      = array_column($rankInfo['rank_list'], 'user_id');
        $userProfiles = UserProfile::getUsersProfile($userIds, false, true);
        $ret['list']  = [];
        foreach ($userProfiles as $k => $profile) {
            if (!UserProfile::checkRoleHost($profile['role'])) continue;
            $ret['list'][] = [
                'rank' => $k + 1,
                'name' => $profile['nick_name'],
                'num'  => $rankInfo['rank_list'][$k]['num'],
                'img'  => UserProfile::buildAvatar($profile['avatar'], 150),
            ];
        }
        $ret['list'] = array_slice($ret['list'], 0, 20);
        if (!$profileId) {
            $ret['my_info'] = [
                'rank'   => 0,
                'name'   => '昵称',
                'num'    => 0,
                'img'    => AliyunUtil::IMG_HOST . 'default/ma_avatar.jpeg',
                'gender' => 1,
                'role'   => 1
            ];
        } else {
            $ret['my_info'] = [
                'rank'   => $rankInfo['my_rank'],
                'name'   => $selfProfile->nick_name,
                'num'    => $rankInfo['my_num'],
                'img'    => UserProfile::buildAvatar($selfProfile->avatar, 150),
                'gender' => $selfProfile->gender,
                'role'   => $selfProfile->role,
            ];
        }

        if (count($ret['list']) >= 2) {
            $tmp            = $ret['list'][1];
            $ret['list'][1] = $ret['list'][0];
            $ret['list'][0] = $tmp;
        }

        $ret['act_time'] = $time;

        return $ret;
    }
}
